//
//  WPResultTableView.m
//  wobingwoyi
//
//  Created by WGP on 16/9/6.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPResultTableView.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "WPSearchCaseCell.h"
#import "WPSearchHistoryCell.h"

@interface WPResultTableView ()<UITableViewDelegate,UITableViewDataSource>


@end
@implementation WPResultTableView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = GlobalBgColor;
        self.delegate = self;
        self.dataSource = self;
        self.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self registerClass:[WPSearchHistoryCell class] forCellReuseIdentifier:@"WPSearchHistoryCell"];
        [self registerClass:[WPSearchCaseCell class] forCellReuseIdentifier:@"WPSearchCaseCell"];
    }
    return self;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.models.count + 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == 0)
    {
        WPSearchHistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WPSearchHistoryCell"];
        cell.isHideLine = YES;
        cell.textLabel.text = @"相关病例";
        cell.textLabel.textColor = RGB_SAME(102);
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        return cell;
    }
    else
    {
        WPSearchCaseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WPSearchCaseCell"];
        cell.searchStr = self.searchStr;
        cell.model = self.models[indexPath.item-1];
        cell.tapTriggered = ^{
            
            if (self.didSelectedBlock)
            {
                self.didSelectedBlock(indexPath);
            }
        };
        return cell;
    }

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.didSelectedBlock)
    {
        self.didSelectedBlock(indexPath);
    }
}

-(void)setModels:(NSArray *)models
{
    _models = models;
    
    [self reloadData];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == 0)
    {
        return 44;
    }
    else
    {
        return [tableView fd_heightForCellWithIdentifier:@"WPSearchCaseCell" cacheByIndexPath:indexPath configuration:^(WPSearchCaseCell *cell) {
            // 配置 cell 的数据源，和 "cellForRow" 干的事一致，比如：
            cell.searchStr  = self.searchStr;
            cell.model = self.models[indexPath.item-1];
        }];
    }
}
@end
